Page({
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseInfo: '', //报修房屋
    repairItemName: '', //报修项目
    mobile: '',  //手机号码
    appointment: '',  //预约日期
    description: '', //问题描述
    houseList: [],
    repairItem: [{ name: '水路卫浴' }, { name: '电路灯具' }, { name: '管道疏通' }, { name: '开锁换锁' }],
    attachment: [],
  },
  onLoad({ id }) {
    this.getHouseList()
    // 获取维修项目
    this.getrepairItemList()
    if (id) {
      wx.setNavigationBarTitle({
        title: '修改报修信息'
      })
      this.getRepairDetail(id)
    }
  },
  //获取报修详情
  async getRepairDetail(id) {
    const res = await wx.http.get(`/repair/${id}`)
    console.log(res, '详情');
    this.setData({
      ...res.data
    })
  },
  //选择保修房屋
  selectHouse(e) {
    console.log(e);

    const { name, id: houseId } = e.detail
    this.setData({
      houseInfo: name, houseId
    })
  },
  //选择报修项目
  selectRepairItem(e) {
    const { name, id: repairItemId } = e.detail
    this.setData({
      repairItemName: name, repairItemId
    })
  },
  //校验手机号码
  checkPhome() {
    const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    const vaild = reg.test(this.checkPhome.trim())
    if (!vaild) return wx.utils.toast('请输入合法的手机号码')
    return vaild
  },
  //选择时间
  selectDate(e) {
    // console.log(e, 11111);
    // const time = e.detail
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formDate(e.detail)
    })
  },
  //选择报修房屋下拉框
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  //关闭选择报修房屋下拉框
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  // 选择维修项目下拉框
  openRepairLayer() {
    this.setData({ repairLayerVisible: true })
  },
  // 关闭选择维修项目下拉框
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },
  //选择上门维修日期
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  //关闭选择上门维修日期
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  //上传图片
  afterRead(e) {
    const { file } = e.detail
    console.log(file)
    wx.uploadFile({
      url: 'https://live-api.itheima.net/upload',
      filePath: file.url,
      name: 'file',
      header: {
        Authorization: `Bearer ${getApp().token}`
      },
      success: (res) => {
        //转换JSON数据
        const obj = JSON.parse(res.data)
        // 上传完成需要更新为空
        const { attachment = [] } = this.data
        attachment.push({ ...obj.data })
        // 将数据存入到attachment中
        this.setData({ attachment })
      }
    })
  },
  //删除图片
  delete(e) {
    console.log(e.detail.index);
    const { attachment } = this.data
    attachment.splice(e.detail.index, 1)
    this.setData({ attachment })
  },
  //获取审核通过的房屋
  async getHouseList() {
    const { data } = await wx.http.get('/house')
    // console.log(data, 5555);
    this.setData({
      houseList: data
    })
  },
  //获取维修项目列表
  async getrepairItemList() {
    const { data } = await wx.http.get('/repairItem')
    this.setData({
      repairItem: data
    })
  },
  //校验报修房屋
  checkHouse() {
    const vaild = Boolean(this.data.houseInfo)
    if (!vaild) return wx.utils.toast('请选择报修房屋!')
    return vaild
  },
  //校验维修项目
  checkRepair() {
    const vaild = Boolean(this.data.repairItemName)
    if (!vaild) return wx.utils.toast('请选择维修项目!')
    return vaild
  },
  //校验手机号码
  checkmobile() {
    const reg = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    const vaild = reg.test(this.data.mobile.trim())
    if (!vaild) return wx.utils.toast('请输入合法的手机号码!')
    return vaild
  },
  //校验预约日期
  checkDate() {
    // 验证日期格式
    const reg = /^\d{4}[- | /]\d{2}[- | /]\d{2}$/
    const vaild = reg.test(this.data.appointment)
    if (!vaild) return wx.utils.toast('请选择预约日期!')
    return vaild
  },
  //校验问题描述
  checkDesc() {
    const vaild = this.data.description
    if (!vaild) return wx.utils.toast('请输入问题描述!')
    return vaild
  },
  //校验问题附件
  checkPic() {
    const vaild = this.data.attachment.length
    if (!vaild) return wx.utils.toast('请上传问题附件!')
    return vaild
  },
  //点击提交报修按钮
  async onSubmit() {
    if (!this.checkHouse()) return
    if (!this.checkRepair()) return
    if (!this.checkmobile()) return
    if (!this.checkDate()) return
    if (!this.checkDesc()) return
    if (!this.checkPic()) return
    //解构请求需要的参数
    const { id, houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    await wx.http.post('/repair', { id, houseId, repairItemId, mobile, appointment, description, attachment })
    // 提示
    wx.utils.toast('报修成功')
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  },
})
